import Compressor from 'compressorjs';
// 只能对jpeg格式的图片进行转换
/**
 * @param image 图片
 * @param backType 需要返回的类型blob,file
 * @param quality 图片压缩比 0-1,数字越小，图片压缩越小
 * @returns
 */
export default function ImageCompressor(image, backType, quality) {
  return new Promise((resolve, reject) => {
    new Compressor(image, {
      quality: quality || 0.6,
      success(result) {
        let file = new File([result], image.name, { type: image.type });
        if (!backType || backType == 'blob') {
          resolve(result);
        } else if (backType == 'file') {
          resolve(file);
        } else {
          resolve(file);
        }
      },
      error(err) {
        reject(err);
      },
    });
  });
}
